package com.cip.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.cip.client.modelo.SimpleGwtRPCDSRecord;
import com.cip.client.servicios.SimpleGwtRPCDSService;
import com.cip.server.utiles.PMF;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * 
 * @author Aleksandras Novikovas
 * @author System Tier
 * @version 1.0
 */
public class SimpleGwtRPCDSServiceImpl extends RemoteServiceServlet implements
		SimpleGwtRPCDSService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8376799538876959675L;

	/*
	 * static List<SimpleGwtRPCDSRecord> list;
	 * 
	 * static int id; static { id = 1; list = new
	 * ArrayList<SimpleGwtRPCDSRecord>(); SimpleGwtRPCDSRecord record; record =
	 * new SimpleGwtRPCDSRecord(); record.setId(id++); record.setName("First");
	 * // record.setDate(new Date()); list.add(record); record = new
	 * SimpleGwtRPCDSRecord(); record.setId(id++); record.setName("Second"); //
	 * record.setDate(new Date()); list.add(record); record = new
	 * SimpleGwtRPCDSRecord(); record.setId(id++); record.setName("Third"); //
	 * record.setDate(new Date()); list.add(record); }
	 */
	public List<SimpleGwtRPCDSRecord> fetch() {
		List<SimpleGwtRPCDSRecord> l = new ArrayList<SimpleGwtRPCDSRecord>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<SimpleGwtRPCDSRecord> l2 = new ArrayList<SimpleGwtRPCDSRecord>();
		pm.setDetachAllOnCommit(true); 
		Query q = pm.newQuery(SimpleGwtRPCDSRecord.class);

		
		try {
		
			l = (List<SimpleGwtRPCDSRecord>) q.execute();
			
		//	l2.addAll(l);	
			if (l.iterator().hasNext()) {
	            for (SimpleGwtRPCDSRecord e : l) {
	            	
	            	System.out.println("ob:"+e.toString());
	            	
	               l2.add(pm.detachCopy(e));
	            }
	        } else {
	        	System.out.println("vacio");
	        }
			//l2 = (List<SimpleGwtRPCDSRecord>) pm.detachCopyAll(l);
			
			System.out.println("list:"+l.toString());
		} finally {
			q.closeAll();
			pm.close();
		}
	/*	SimpleGwtRPCDSRecord aa = new SimpleGwtRPCDSRecord();
		aa.setId("112");
		aa.setName("asdf");*/
	
		return l2;
	}

	public SimpleGwtRPCDSRecord add(SimpleGwtRPCDSRecord record) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SimpleGwtRPCDSRecord n = new SimpleGwtRPCDSRecord();
	//	pm.setDetachAllOnCommit(true); 
		
		try {
			n.setId(null);
		//	n.setLista(null);
			n.setName(record.getName());
			pm.makePersistent(record);
		//	pm.flush();
			n = pm.detachCopy(record);
		} finally {
			pm.close();
		}
		return n;
	}

	public SimpleGwtRPCDSRecord update(SimpleGwtRPCDSRecord record) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			// SimpleGwtRPCDSRecord e = pm.getObjectById(record.getClass(),
			// record.getId());
			// e.copia(record);

		} finally {
			pm.close();
		}
		return null;
	}

	public void remove(SimpleGwtRPCDSRecord record) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			long deleteCount = 0;
			Query q = pm.newQuery(record.getClass());
			List<SimpleGwtRPCDSRecord> stocks = (List<SimpleGwtRPCDSRecord>) q
					.execute();

			for (SimpleGwtRPCDSRecord stock : stocks) {
				if (record.getId().equals(stock.getId())) {
					deleteCount++;
					pm.deletePersistent(stock);
				}
			}
			if (deleteCount != 1) {
				System.out.println("removeStock deleted " + deleteCount
						+ " Stocks");
			}
		} finally {
			pm.close();
		}
	}

}